package org2.jogo;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

public class Sound {

	private static MediaPlayer bgMusic;
	private static MediaPlayer[] sounds;
	private static boolean sonsHabilitados = true;

	public static void iniciaMusicaFundo(Context ctx, int musicaId) {
		try {
			if (bgMusic != null) {
				bgMusic.stop();
				bgMusic.release();
			}
			bgMusic = new MediaPlayer();
			Uri path = Uri.parse("android.resource://org2.jogo/" + musicaId);
			bgMusic.setDataSource(ctx, path);
			bgMusic.setAudioStreamType(AudioManager.STREAM_MUSIC);
			bgMusic.setLooping(true);
			bgMusic.prepareAsync();
			bgMusic.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				public void onPrepared(MediaPlayer mp) {
					if (sonsHabilitados)
						mp.start();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static MediaPlayer getSom(int id) {
		if (sounds == null)
			sounds = new MediaPlayer[10];
		return sounds[id];
	}

	public static MediaPlayer iniciaSom(Context ctx, int mediaId, int somId, boolean loop) {

		try {
			if (sounds == null)
				sounds = new MediaPlayer[10];
			final MediaPlayer som = MediaPlayer.create(ctx, mediaId);
			som.setLooping(loop);
			if (sounds[somId] != null)
				sounds[somId].release();
			sounds[somId] = som;
			if (sonsHabilitados)
				som.start();
			som.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {
					som.release();
				}
			});
			return som;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void pararSom(int somId) {
		if (sounds != null && sounds[somId] != null) {
			sounds[somId].release();
		}
	}

	public static void pararMusicaFundo() {
		if (bgMusic != null) {
			bgMusic.stop();
			bgMusic.release();
		}
	}

	public static void tocarSom(Context ctx, int somId) {
		try {
			final MediaPlayer som = MediaPlayer.create(ctx, somId);
			if (sonsHabilitados)
				som.start();
			som.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {
					som.release();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void pausarTodosOsSons() {
		try {
			if (bgMusic != null)
				bgMusic.pause();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (sounds != null) {
			for (int i = 0; i < 10; i++) {
				try {
					if (sounds[i] != null)
						sounds[i].pause();
				} catch (Exception ex) {
				}
			}
		}
	}

	public static void continuarTodosOsSons() {
		if (sonsHabilitados) {
			try {
				if (bgMusic != null)
					bgMusic.start();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (sounds != null) {
				for (int i = 0; i < 10; i++) {
					try {
						if (sounds[i] != null)
							sounds[i].start();
					} catch (Exception ex) {
					}
				}
			}
		}
	}

	public static void habilitarSons(boolean habilitados) {
		sonsHabilitados = habilitados;
		if (habilitados)
			continuarTodosOsSons();
		else
			pausarTodosOsSons();
	}
}
